﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.Announcement.ManageAnnounce
{
    public partial class ManageAnnounceUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindControls();
            }

        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            string subject = this.txtSubject.Text;
            string status = string.Empty;
            string readStatus = string.Empty;

            DateTime? start = null;
            DateTime? end = null;

            if (!dtEffDateFrom.IsDateEmpty)
                start = this.dtEffDateFrom.SelectedDate;

            if (!dtEffDateTo.IsDateEmpty)
                end = this.dtEffDateTo.SelectedDate;

            status = this.rblStatus.SelectedValue == "All" ? string.Empty : this.rblStatus.SelectedValue;
            readStatus = this.rbtReadStatus.SelectedValue == "All" ? string.Empty:this.rbtReadStatus.SelectedValue;

            // status
            //if (this.ddlStatus.SelectedIndex == 1)
            //{
            //    status = JCYConstant.Status.Draft;
            //}
        
            //else if (this.ddlStatus.SelectedIndex == 2)
            //{
            //    status = JCYConstant.Status.Publish;
            //}
            // read status
            //if (this.ddlReadStatus.SelectedIndex == 1)
            //{
            //    readStatus = JCYConstant.ReadStatus.Read;
            //}
            //else if (this.ddlReadStatus.SelectedIndex == 2)
            //{
            //    readStatus = JCYConstant.ReadStatus.UnRead;
            //}

            using (AnnounceProxy proxy = new AnnounceProxy())
            {
                AnnounceDataSet.T_Announce_SearchDataTable searchDT = proxy.SearchAnnounce(subject, status, readStatus, start, end);
                this.gvBC.DataSource = searchDT;
                this.gvBC.DataBind();
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(SPContext.Current.Web.Url);
        }

        //private void BindControls()
        //{
        //    this.ddlStatus.Items.Insert(0, new ListItem(JCYConstant.DropdownlistEmptyText, ""));
        //    this.ddlStatus.Items.Insert(1, "草稿");
        //    this.ddlStatus.Items.Insert(2, "已发布");

        //    this.ddlReadStatus.Items.Insert(0, new ListItem(JCYConstant.DropdownlistEmptyText, ""));
        //    this.ddlReadStatus.Items.Insert(1, "已阅读");
        //    this.ddlReadStatus.Items.Insert(2, "未阅读");
        //}

        protected void gvBC_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblReadStatus = (Label)e.Row.Cells[3].FindControl("lblReadStatus");
                if (lblReadStatus.Text.Equals(JCYConstant.ReadStatus.Read))
                    lblReadStatus.Text = "已阅读";
                else
                    lblReadStatus.Text = "未阅读";

                // load date format
                Label lblEffDate = (Label)e.Row.Cells[0].FindControl("lblEffDate");

                string dateString = lblEffDate.Text;
                DateTime dateTime = DateTime.Parse(dateString);
                lblEffDate.Text = dateTime.ToString(JCYConstant.Date_Format_CN);
            }

        }

    }
}
